package com.etrans.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

/**
 * Http与Servlet工具类 ServletUtils.java Create on 2014-4-28
 * @author dasuan
 */
public class ServletUtils {

	// Content Type 定义
	public static final String TEXT_TYPE = "text/plain";
	public static final String JSON_TYPE = "application/json";
	public static final String XML_TYPE = "text/xml";
	public static final String HTML_TYPE = "text/html";
	public static final String JS_TYPE = "text/javascript";
	public static final String EXCEL_TYPE = "application/vnd.ms-excel";

	// Header 定义
	public static final String AUTHENTICATION_HEADER = "Authorization";

	// 常用数值定义
	public static final long ONE_YEAR_SECONDS = 60 * 60 * 24;

	/**
	 * 设置客户端缓存过期时间 Header
	 * @author dasuan
	 * @version 1.0
	 * @param response 响应对象
	 * @param expiresSeconds 超时时间 s
	 */
	public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) {
		// Http 1.1 header
		response.setHeader("Cache-Control", "private, max-age=" + expiresSeconds);
	}

	/**
	 * 设置客户端无缓存Header
	 * @author dasuan
	 * @version 1.0
	 * @param response 响应对象
	 */
	public static void setNoCacheHeader(HttpServletResponse response) {
		// Http 1.1 header
		response.setHeader("Cache-Control", "no-cache");
	}

	/**
	 * 设置让浏览器弹出下载对话框的Header
	 * @author dasuan
	 * @version 1.0
	 * @param response 响应对象
	 * @param fileName 下载后的文件名
	 */
	public static void setFileDownloadHeader(HttpServletResponse response, String fileName) {
		try {
			// 中文文件名支持
			String encodedfileName = new String(fileName.getBytes(), "ISO8859-1");
			response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
		} catch (UnsupportedEncodingException e) {
		}
	}

}
